class Solution {
public:
    bool isInterleave(string s1, string s2, string s3)
    {
        if (s1 == "") return s2 == s3;
        if (s2 == "") return s1 == s3;
        if (s1.size() + s2.size() != s3.size()) return false;
        int m = s1.size(), n = s2.size();
        s1 = " " + s1;
        s2 = " " + s2;
        s3 = " " + s3;
        vector<vector<bool>> dp(m + 1, vector<bool>(n + 1));
        dp[0][0] = true;
        for (int i = 1; i <= m; i++)
        {
            if (s1[i] == s3[i]) dp[i][0] = true;
            else break;
        }
        for (int j = 1; j <= n; j++)
        {
            if (s2[j] == s3[j]) dp[0][j] = true;
            else break;
        }

        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (s1[i] == s3[i + j] && dp[i - 1][j]) dp[i][j] = true;
                if (s2[j] == s3[i + j] && dp[i][j - 1]) dp[i][j] = true;
                //dp[i][j] = (s1[i] == s3[i+j] && dp[i-1][j]) || ((s2[j] == s3[i+j]) && dp[i][j-1]);
            }
        }
        return dp[m][n];
    }
};
class Solution {
public:
    bool isMatch(string s, string p)
    {
        int m = p.size(), n = s.size();
        vector<vector<bool>> dp(m + 1, vector<bool>(n + 1));
        dp[0][0] = true;
        s = ' ' + s;
        p = ' ' + p;
        for (int i = 2; i <= m; i += 2)
        {
            if (p[i] == '*') dp[i][0] = true;
            else break;
        }

        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (p[i] == '*')
                    dp[i][j] = dp[i - 2][j] || ((p[i - 1] == '.' || p[i - 1] == s[j]) && dp[i][j - 1]);
                else
                    dp[i][j] = (p[i] == '.' || p[i] == s[j]) && dp[i - 1][j - 1];
            }
        }
        return dp[m][n];
    }
};
class Solution {
public:
    bool isMatch(string s, string p)
    {
        int m = p.size(), n = s.size();
        s = ' ' + s;
        p = ' ' + p;
        vector<vector<bool>> dp(m + 1, vector<bool>(n + 1));
        dp[0][0] = true;
        for (int i = 1; i <= m; i++)
        {
            if (p[i] == '*') dp[i][0] = true;
            else break;
        }

        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (p[i] == '*') dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
                else if (p[i] == '?') dp[i][j] = dp[i - 1][j - 1];
                else dp[i][j] = (p[i] == s[j] && dp[i - 1][j - 1]);
            }
        }
        return dp[m][n];
    }
};